package com.hilaver.dzmis.service.impl;

import java.util.Iterator;
import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.codetable.CtDesignType;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.codetable.CtYesno;
import com.hilaver.dzmis.dao.OrderYarnDAO;
import com.hilaver.dzmis.order.OrderYarn;
import com.hilaver.dzmis.order.OrderYarnLot;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class OrderYarnServiceImpl extends AbstractBaseService {
	
	private CtServiceImpl ctService;
	
	private OrderYarnDAO dao;
	
	private BiDZColorServiceImpl biDZColorServiceImpl;
	
	public OrderYarnServiceImpl(){
		this.dao = new OrderYarnDAO();
		this.ctService = new CtServiceImpl();
		this.biDZColorServiceImpl = new BiDZColorServiceImpl();
	}
	
	public String edit(OrderYarn obj) throws Exception {
		String operation = null;
		if(obj.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}
	
	public String edit(OrderYarnLot lot) throws Exception{
		if(lot.getId() != null){
			OrderYarnLot old = (OrderYarnLot)this.dao.get(OrderYarnLot.class.getName(), lot.getId());
			if(lot.getPhotoName() == null){
				lot.setPhoto(old.getPhoto());
				lot.setPhotoName(old.getPhotoName());
			}
			
			this.dao.getSession().evict(old);
		}
		this.dao.saveOrUpdate(lot);
		//this.dao.storeSysMbox(lot);
		return "{success: true}";
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(OrderYarn.class.getName(), id);
		return super.delete(OrderYarn.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		OrderYarn oy = (OrderYarn)this.dao.get(OrderYarn.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(this.toXMLFragment(oy));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getLot(int id) throws Exception {
		OrderYarnLot oyl = (OrderYarnLot)this.dao.get(OrderYarnLot.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		writeFile(realFileDir + oyl.getPhotoName(), oyl.getPhoto());
		xmlSB.append(SimpleObj2XML.toXMLFragment(oyl));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		OrderYarn oy = (OrderYarn)this.dao.get(OrderYarn.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(this.toXMLFragment(oy));

		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort,
			String order, String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(OrderYarn.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((OrderYarn)obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(OrderYarn.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getLotAll(Integer id) throws Exception {
		String [] filters = new String[1];
		filters[0] = "orderYarn.id=" + id;
		List<OrderYarnLot> lotsList = this.dao.getAll(OrderYarnLot.class.getName(), "lotNumber", "ASC", filters);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (OrderYarnLot obj : lotsList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<isColorConfirmed>");
			xmlSB.append(this.ctService.getDescription(CtYesno.class.getName(), obj.getIsColorConfirmed(), locale));
			xmlSB.append("</isColorConfirmed>");
			
			xmlSB.append("<isQuanlityTested>");
			xmlSB.append(this.ctService.getDescription(CtYesno.class.getName(), obj.getIsQuanlityTested(), locale));
			xmlSB.append("</isQuanlityTested>");
			
			writeFile(realFileDir + obj.getPhotoName(), obj.getPhoto());
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(OrderYarnLot.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAll(Integer piId) throws Exception{
		
		return null;
	}

	public String getMaxReferenceIndex() throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex();
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("000001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to6Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String toXMLFragment(OrderYarn oy) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
		
		int lotQuantity;
		Float quantity;
		if(oy.getOrderYarnLots().size() == 0){
			lotQuantity = 0;
			quantity = 0f;
		}else{
			lotQuantity = oy.getOrderYarnLots().size();
			quantity = 0f;
			for (Iterator iterator = oy.getOrderYarnLots().iterator(); iterator.hasNext();) {
				OrderYarnLot lot = (OrderYarnLot) iterator.next();
				quantity += lot.getQuantity();
			}
		}
		
		xmlSB.append("<lotQuantity>");
		xmlSB.append(lotQuantity);
		xmlSB.append("</lotQuantity>");
		xmlSB.append("<quantity>");
		xmlSB.append(quantity);
		xmlSB.append("</quantity>");
		
		xmlSB.append("<ctSeasonNumber>");
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), oy.getCtSeasonNumber(), locale));
		xmlSB.append("</ctSeasonNumber>");
		
		
		BiDZColor biDZColor = oy.getBiDZColor();
		
		writeFile(realFileDir + biDZColor.getPhoto1Name(), biDZColor.getPhoto1());
		writeFile(realFileDir + biDZColor.getPhoto2Name(), biDZColor.getPhoto2());
		
		xmlSB.append("<dzColorId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</dzColorId>");
		
		xmlSB.append("<dzColorCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getDzColorCode());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</dzColorCode>");
		
		if(biDZColor.getBiFranceCustomer() != null){
			xmlSB.append("<customerId>");
			xmlSB.append(biDZColor.getBiFranceCustomer().getId());
			xmlSB.append("</customerId>");
			
			xmlSB.append("<customerName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerName>");
			
			xmlSB.append("<customerDepartment>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiFranceCustomer().getDepartment()==null?"":biDZColor.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerDepartment>");
			
			xmlSB.append("<customerColorCode>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getCustomerColorCode() == null? "" : biDZColor.getCustomerColorCode());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerColorCode>");
		}
		
		if(biDZColor.getBiYarn().getBiFactory() != null){
			xmlSB.append("<factoryId>");
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getId());
			xmlSB.append("</factoryId>");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameEN()==null?"":biDZColor.getBiYarn().getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<factoryName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(Constants.LOCALE_TYPE_ZH.equals(this.locale)){
				xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameCN());
			}else{
				xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameEN()==null?"":biDZColor.getBiYarn().getBiFactory().getNameEN());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryName>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
			
			xmlSB.append("<supplierNumber>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getSupplierNumber() == null? "":biDZColor.getSupplierNumber());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</supplierNumber>");
		}
		
		xmlSB.append("<pantoneNumber>");
		xmlSB.append(biDZColor.getPantoneNumber() == null? "":biDZColor.getPantoneNumber());
		xmlSB.append("</pantoneNumber>");
		
		xmlSB.append("<realColor>");
		xmlSB.append(biDZColor.getRealColor()  == null? "":biDZColor.getRealColor());
		xmlSB.append("</realColor>");
		
		xmlSB.append("<ctDesignType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtDesignType.class.getName(), oy.getCtDesignType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctDesignType>");
		
		xmlSB.append("<yarnId>");
		xmlSB.append(biDZColor.getBiYarn().getId());
		xmlSB.append("</yarnId>");
		
		xmlSB.append("<yarnReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiYarn().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnReference>");
		
		xmlSB.append("<ctYarnMeasure>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMeasure.class.getName(), biDZColor.getBiYarn().getCtYarnMeasure(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMeasure>");
		
		xmlSB.append("<yarnAccount>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiYarn().getYarnAccount());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnAccount>");
		
		xmlSB.append("<ctYarnMaterial1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial1(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial1()) && biDZColor.getBiYarn().getComposition1() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition1() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial1>");
		
		xmlSB.append("<ctYarnMaterial2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial2(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial2()) && biDZColor.getBiYarn().getComposition2() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition2() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial2>");
		
		xmlSB.append("<ctYarnMaterial3>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial3(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial3()) && biDZColor.getBiYarn().getComposition3() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition3() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial3>");
		
		xmlSB.append("<ctYarnMaterial4>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial4(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial4()) && biDZColor.getBiYarn().getComposition4() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition4() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial4>");
		
		xmlSB.append("<ctYarnMaterial5>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial5(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial5()) && biDZColor.getBiYarn().getComposition5() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition5() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial5>");
		
		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biDZColor.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biDZColor.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biDZColor.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biDZColor.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		biDZColorServiceImpl.setRealFileDir(this.realFileDir);
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColorServiceImpl.getIsColorApprovedPhoto(biDZColor));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(oy));

		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		OrderYarnServiceImpl os = new OrderYarnServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}

}
